package com.wva.video.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wva.common.context.UserContext;
import com.wva.common.domain.dto.PageQueryDTO;
import com.wva.common.domain.vo.PageQueryVO;
import com.wva.video.domain.po.Video;
import com.wva.video.domain.po.VideoCollect;
import com.wva.video.domain.vo.VideoItemDTO;
import com.wva.video.enums.RedisKey;
import com.wva.video.mapper.VideoCollectMapper;
import com.wva.video.service.VideoCollectService;
import com.wva.video.service.VideoService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hong
 * @since 2025-03-09
 */
@Service
public class VideoCollectServiceImpl extends ServiceImpl<VideoCollectMapper, VideoCollect> implements VideoCollectService {
    @Resource
    StringRedisTemplate stringRedisTemplate;
    @Resource
    VideoCollectMapper videoCollectMapper;
    @Resource
    VideoService videoService;

    @Override
    @Async
    public void collect(Long videoId) {
        String key = RedisKey.COLLECT_SET.format(videoId);
        stringRedisTemplate.opsForSet().add(key, String.valueOf(UserContext.getUserId()));
        //持久化到数据库
        VideoCollect videoCollect = new VideoCollect();
        videoCollect.setVideoId(videoId);
        videoCollect.setUserId(UserContext.getUserId());
        save(videoCollect);
    }

    @Override
    @Async
    public void cancelCollect(Long videoId) {
        String key = RedisKey.COLLECT_SET.format(videoId);
        stringRedisTemplate.opsForSet().remove(key, String.valueOf(UserContext.getUserId()));
        //持久化到数据库
        lambdaUpdate()
                .eq(VideoCollect::getUserId, UserContext.getUserId())
                .eq(VideoCollect::getVideoId, videoId)
                .remove();
    }

    @Override
    public Long getVideoCollectCountByVideoId(Long videoId) {
        String key = RedisKey.COLLECT_SET.format(videoId);
        return Optional.ofNullable(stringRedisTemplate.opsForSet().size(key)).orElse(0L);
    }

    @Override
    public Boolean isCollectVideoByVideoId(Long videoId) {
        String key = RedisKey.COLLECT_SET.format(videoId);
        Boolean like = stringRedisTemplate.opsForSet().isMember(key, String.valueOf(UserContext.getUserId()));
        return Boolean.TRUE.equals(like);
    }

    @Override
    public PageQueryVO<VideoItemDTO> listUserCollectVideoByPage(PageQueryDTO pageQueryDTO) {
        //初始化
        Integer pageNo = pageQueryDTO.getPageNo();
        Integer pageSize = pageQueryDTO.getPageSize();
        String sortBy = pageQueryDTO.getSortBy();
        Boolean desc = pageQueryDTO.getDesc();

        //构造mp分页器
        try (Page<Object> page = PageHelper.startPage(pageNo, pageSize);
             Page<Video> pages = videoCollectMapper.listUserCollectVideoByPage(UserContext.getUserId());
        ) {
            List<Video> result = pages.getResult();
            List<VideoItemDTO> videoItemDTOS = result.stream().map(video -> BeanUtil.copyProperties(video, VideoItemDTO.class))
                    .collect(Collectors.toList());

            //包装
            videoService.wrapVideoItemDTOList(videoItemDTOS);

            PageQueryVO<VideoItemDTO> pageQueryVO = new PageQueryVO<>();
            pageQueryVO.setTotal(pages.getTotal());
            pageQueryVO.setPages((long) pages.getPages());
            pageQueryVO.setList(videoItemDTOS);
            return pageQueryVO;
        }
    }
}
